<?php


namespace addons\mental\model;

use think\Model;


class Remark extends Model
{
    // 表名
    protected $name = 'mental_remark';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [];

    /**
     * Undocumented function
     * 列表
     * @return Object
     */
    public function tableList($param)
    {
        $limit = 10;
        if (isset($param['limit'])) {
            $limit = $param['limit'];
        }
        $whereArr = $this->tableWhere($param);
        return $this->with('User')->field($whereArr['field'])
            ->where($whereArr['where'])
            ->order($whereArr['order'])
            ->paginate($limit);
    }


    /**
     * Undocumented function
     * 统一处理查询条件
     * @param [type] $param
     * @return void
     */
    public function tableWhere($param)
    {
        $where = [];
        $where['is_audit'] = 1;
        if (isset($param['user_id']) && !empty($param['user_id'])) {
            $where['user_id'] = $param['user_id'];
        }
        if (isset($param['main_id']) && !empty($param['main_id'])) {
            $where['main_id'] = $param['main_id'];
        }
        $result['where'] = $where;
        $result['field'] = "*";
        $result['order'] = [];
        return $result;
    }

    public function User()
    {
        return $this->hasOne('User', 'id', 'user_id')->bind('avatar,nickname');
    }
}
